<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'shop:order:soMain:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.ddate }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopOrderSoMainList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { soMainDelete, soMainListData } from '/@/api/shop/order/soMain';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('shop.order.soMain');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('销售订单管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('单据日期'),
        field: 'ddate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('业务类型'),
        field: 'cbustype',
        component: 'Input',
      },
      {
        label: t('销售类型'),
        field: 'cstcode',
        component: 'Input',
      },
      {
        label: t('客户编码'),
        field: 'cusCode',
        component: 'Input',
      },
      {
        label: t('部门'),
        field: 'officeCode',
        component: 'Input',
      },
      {
        label: t('业务员'),
        field: 'personCode',
        component: 'Input',
      },
      {
        label: t('币种'),
        field: 'cexchName',
        component: 'Input',
      },
      {
        label: t('汇率'),
        field: 'iexchrate',
        component: 'Input',
      },
      {
        label: t('税率'),
        field: 'itaxrate',
        component: 'Input',
      },
      {
        label: t('退货标志'),
        field: 'backFlag',
        component: 'Input',
      },
      {
        label: t('单据模板'),
        field: 'ivtid',
        component: 'Input',
      },
      {
        label: t('备注信息'),
        field: 'remarks',
        component: 'Input',
      },
      {
        label: t('自定义项1'),
        field: 'cdefine1',
        component: 'Input',
      },
      {
        label: t('自定义项2'),
        field: 'cdefine2',
        component: 'Input',
      },
      {
        label: t('自定义项3'),
        field: 'cdefine3',
        component: 'Input',
      },
      {
        label: t('自定义项4'),
        field: 'cdefine4',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('自定义项5'),
        field: 'cdefine5',
        component: 'Input',
      },
      {
        label: t('自定义项6'),
        field: 'cdefine6',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('自定义项7'),
        field: 'cdefine7',
        component: 'Input',
      },
      {
        label: t('自定义项8'),
        field: 'cdefine8',
        component: 'Input',
      },
      {
        label: t('自定义项9'),
        field: 'cdefine9',
        component: 'Input',
      },
      {
        label: t('自定义项10'),
        field: 'cdefine10',
        component: 'Input',
      },
      {
        label: t('自定义项11'),
        field: 'cdefine11',
        component: 'Input',
      },
      {
        label: t('自定义项12'),
        field: 'cdefine12',
        component: 'Input',
      },
      {
        label: t('自定义项13'),
        field: 'cdefine13',
        component: 'Input',
      },
      {
        label: t('自定义项14'),
        field: 'cdefine14',
        component: 'Input',
      },
      {
        label: t('自定义项15'),
        field: 'cdefine15',
        component: 'Input',
      },
      {
        label: t('自定义项16'),
        field: 'cdefine16',
        component: 'Input',
      },
      {
        label: t('子公司店面属性'),
        field: 'chdefine1',
        component: 'Input',
      },
      {
        label: t('战区分类'),
        field: 'chdefine2',
        component: 'Input',
      },
      {
        label: t('合作方式'),
        field: 'chdefine3',
        component: 'Input',
      },
      {
        label: t('市级代理'),
        field: 'chdefine4',
        component: 'Input',
      },
      {
        label: t('区级代理'),
        field: 'chdefine5',
        component: 'Input',
      },
      {
        label: t('招商类别'),
        field: 'chdefine6',
        component: 'Input',
      },
      {
        label: t('招商主体'),
        field: 'chdefine7',
        component: 'Input',
      },
      {
        label: t('运营主管'),
        field: 'chdefine8',
        component: 'Input',
      },
      {
        label: t('省区经理'),
        field: 'chdefine9',
        component: 'Input',
      },
      {
        label: t('市场总监'),
        field: 'chdefine10',
        component: 'Input',
      },
      {
        label: t('战区总经理'),
        field: 'chdefine11',
        component: 'Input',
      },
      {
        label: t('招商总监'),
        field: 'chdefine12',
        component: 'Input',
      },
      {
        label: t('招商经理'),
        field: 'chdefine13',
        component: 'Input',
      },
      {
        label: t('物流公司'),
        field: 'chdefine14',
        component: 'Input',
      },
      {
        label: t('经销商收货地址'),
        field: 'chdefine15',
        component: 'Input',
      },
      {
        label: t('经销商名称'),
        field: 'chdefine16',
        component: 'Input',
      },
      {
        label: t('经销商简称'),
        field: 'chdefine17',
        component: 'Input',
      },
      {
        label: t('收货联系人'),
        field: 'chdefine18',
        component: 'Input',
      },
      {
        label: t('经销商返利余额'),
        field: 'chdefine19',
        component: 'Input',
      },
      {
        label: t('经销商余额'),
        field: 'chdefine20',
        component: 'Input',
      },
      {
        label: t('核发店面简称'),
        field: 'chdefine21',
        component: 'Input',
      },
      {
        label: t('收货人联系电话'),
        field: 'chdefine22',
        component: 'Input',
      },
      {
        label: t('经销商赠送余额'),
        field: 'chdefine23',
        component: 'Input',
      },
      {
        label: t('是否包邮'),
        field: 'chdefine24',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
          allowClear: true,
        },
      },
      {
        label: t('包邮关联单号'),
        field: 'chdefine25',
        component: 'Input',
      },
      {
        label: t('是否发货'),
        field: 'chdefine26',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
          allowClear: true,
        },
      },
      {
        label: t('实际控股人'),
        field: 'chdefine27',
        component: 'Input',
      },
      {
        label: t('法人'),
        field: 'chdefine28',
        component: 'Input',
      },
      {
        label: t('经销商首发日期'),
        field: 'chdefine29',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('核发店面归属'),
        field: 'chdefine30',
        component: 'Input',
      },
      {
        label: t('物流对接单位'),
        field: 'chdefine31',
        component: 'Input',
      },
      {
        label: t('核发店面首发日期'),
        field: 'chdefine32',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('子公司名称'),
        field: 'chdefine33',
        component: 'Input',
      },
      {
        label: t('客户返利余额'),
        field: 'chdefine34',
        component: 'Input',
      },
      {
        label: t('客户促销返利余额'),
        field: 'chdefine35',
        component: 'Input',
      },
      {
        label: t('可使用返利上限'),
        field: 'chdefine36',
        component: 'Input',
      },
      {
        label: t('扩展自定义项37'),
        field: 'chdefine37',
        component: 'Input',
      },
      {
        label: t('扩展自定义项38'),
        field: 'chdefine38',
        component: 'Input',
      },
      {
        label: t('原出库单号'),
        field: 'chdefine39',
        component: 'Input',
      },
      {
        label: t('所属省级运营中心'),
        field: 'chdefine40',
        component: 'Input',
      },
      {
        label: t('是否运管中心'),
        field: 'chdefine41',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
          allowClear: true,
        },
      },
      {
        label: t('是否占股100%'),
        field: 'chdefine42',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
          allowClear: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('单据日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('业务类型'),
      dataIndex: 'cbustype',
      key: 'a.cbustype',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('销售类型'),
      dataIndex: 'cstcode',
      key: 'a.cstcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('客户编码'),
      dataIndex: 'cusCode',
      key: 'a.cus_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('部门'),
      dataIndex: 'officeCode',
      key: 'a.office_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('业务员'),
      dataIndex: 'personCode',
      key: 'a.person_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('币种'),
      dataIndex: 'cexchName',
      key: 'a.cexch_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('汇率'),
      dataIndex: 'iexchrate',
      key: 'a.iexchrate',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('税率'),
      dataIndex: 'itaxrate',
      key: 'a.itaxrate',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('退货标志'),
      dataIndex: 'backFlag',
      key: 'a.back_flag',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('单据模板'),
      dataIndex: 'ivtid',
      key: 'a.ivtid',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注信息'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项1'),
      dataIndex: 'cdefine1',
      key: 'a.cdefine1',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项2'),
      dataIndex: 'cdefine2',
      key: 'a.cdefine2',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项3'),
      dataIndex: 'cdefine3',
      key: 'a.cdefine3',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项4'),
      dataIndex: 'cdefine4',
      key: 'a.cdefine4',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('自定义项5'),
      dataIndex: 'cdefine5',
      key: 'a.cdefine5',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('自定义项6'),
      dataIndex: 'cdefine6',
      key: 'a.cdefine6',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('自定义项7'),
      dataIndex: 'cdefine7',
      key: 'a.cdefine7',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('自定义项8'),
      dataIndex: 'cdefine8',
      key: 'a.cdefine8',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项9'),
      dataIndex: 'cdefine9',
      key: 'a.cdefine9',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项10'),
      dataIndex: 'cdefine10',
      key: 'a.cdefine10',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项11'),
      dataIndex: 'cdefine11',
      key: 'a.cdefine11',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项12'),
      dataIndex: 'cdefine12',
      key: 'a.cdefine12',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项13'),
      dataIndex: 'cdefine13',
      key: 'a.cdefine13',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项14'),
      dataIndex: 'cdefine14',
      key: 'a.cdefine14',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项15'),
      dataIndex: 'cdefine15',
      key: 'a.cdefine15',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('自定义项16'),
      dataIndex: 'cdefine16',
      key: 'a.cdefine16',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('子公司店面属性'),
      dataIndex: 'chdefine1',
      key: 'a.chdefine1',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('战区分类'),
      dataIndex: 'chdefine2',
      key: 'a.chdefine2',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('合作方式'),
      dataIndex: 'chdefine3',
      key: 'a.chdefine3',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('市级代理'),
      dataIndex: 'chdefine4',
      key: 'a.chdefine4',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('区级代理'),
      dataIndex: 'chdefine5',
      key: 'a.chdefine5',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('招商类别'),
      dataIndex: 'chdefine6',
      key: 'a.chdefine6',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('招商主体'),
      dataIndex: 'chdefine7',
      key: 'a.chdefine7',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('运营主管'),
      dataIndex: 'chdefine8',
      key: 'a.chdefine8',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('省区经理'),
      dataIndex: 'chdefine9',
      key: 'a.chdefine9',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('市场总监'),
      dataIndex: 'chdefine10',
      key: 'a.chdefine10',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('战区总经理'),
      dataIndex: 'chdefine11',
      key: 'a.chdefine11',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('招商总监'),
      dataIndex: 'chdefine12',
      key: 'a.chdefine12',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('招商经理'),
      dataIndex: 'chdefine13',
      key: 'a.chdefine13',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('物流公司'),
      dataIndex: 'chdefine14',
      key: 'a.chdefine14',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('经销商收货地址'),
      dataIndex: 'chdefine15',
      key: 'a.chdefine15',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('经销商名称'),
      dataIndex: 'chdefine16',
      key: 'a.chdefine16',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('经销商简称'),
      dataIndex: 'chdefine17',
      key: 'a.chdefine17',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('收货联系人'),
      dataIndex: 'chdefine18',
      key: 'a.chdefine18',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('经销商返利余额'),
      dataIndex: 'chdefine19',
      key: 'a.chdefine19',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('经销商余额'),
      dataIndex: 'chdefine20',
      key: 'a.chdefine20',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('核发店面简称'),
      dataIndex: 'chdefine21',
      key: 'a.chdefine21',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('收货人联系电话'),
      dataIndex: 'chdefine22',
      key: 'a.chdefine22',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('经销商赠送余额'),
      dataIndex: 'chdefine23',
      key: 'a.chdefine23',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('是否包邮'),
      dataIndex: 'chdefine24',
      key: 'a.chdefine24',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_yes_no',
    },
    {
      title: t('包邮关联单号'),
      dataIndex: 'chdefine25',
      key: 'a.chdefine25',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('是否发货'),
      dataIndex: 'chdefine26',
      key: 'a.chdefine26',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_yes_no',
    },
    {
      title: t('实际控股人'),
      dataIndex: 'chdefine27',
      key: 'a.chdefine27',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('法人'),
      dataIndex: 'chdefine28',
      key: 'a.chdefine28',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('经销商首发日期'),
      dataIndex: 'chdefine29',
      key: 'a.chdefine29',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('核发店面归属'),
      dataIndex: 'chdefine30',
      key: 'a.chdefine30',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('物流对接单位'),
      dataIndex: 'chdefine31',
      key: 'a.chdefine31',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('核发店面首发日期'),
      dataIndex: 'chdefine32',
      key: 'a.chdefine32',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('子公司名称'),
      dataIndex: 'chdefine33',
      key: 'a.chdefine33',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('客户返利余额'),
      dataIndex: 'chdefine34',
      key: 'a.chdefine34',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('客户促销返利余额'),
      dataIndex: 'chdefine35',
      key: 'a.chdefine35',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('可使用返利上限'),
      dataIndex: 'chdefine36',
      key: 'a.chdefine36',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('扩展自定义项37'),
      dataIndex: 'chdefine37',
      key: 'a.chdefine37',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('扩展自定义项38'),
      dataIndex: 'chdefine38',
      key: 'a.chdefine38',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('原出库单号'),
      dataIndex: 'chdefine39',
      key: 'a.chdefine39',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('所属省级运营中心'),
      dataIndex: 'chdefine40',
      key: 'a.chdefine40',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('是否运管中心'),
      dataIndex: 'chdefine41',
      key: 'a.chdefine41',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_yes_no',
    },
    {
      title: t('是否占股100%'),
      dataIndex: 'chdefine42',
      key: 'a.chdefine42',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_yes_no',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑销售订单'),
        onClick: handleForm.bind(this, { code: record.code }),
        auth: 'shop:order:soMain:edit',edit
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除销售订单'),
        popConfirm: {
          title: t('是否确认删除销售订单'),
          confirm: handleDelete.bind(this, { code: record.code }),
        },
        auth: 'shop:order:soMain:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: soMainListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await soMainDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
